package pers.vic.practice.module.student.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
import pers.vic.boot.base.controller.BaseController;

import pers.vic.boot.base.model.BaseResponse;
import pers.vic.boot.base.vo.Select2VO;
import pers.vic.practice.module.student.service.SCourseService;
import pers.vic.practice.module.student.model.SCourse;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @description:课程表 控制层
 * @author Vic.xu
 * @date: 2022-04-12 09:56
 */
@RestController
@RequestMapping("/student/course")
public class SCourseController extends BaseController<SCourseService, SCourse>{


    @GetMapping("/home")
    public ModelAndView home(){
        ModelAndView modelAndView = new ModelAndView("student/course/home");
        return modelAndView;
    }

    @GetMapping("/form")
    public ModelAndView form(Integer id){
        ModelAndView modelAndView = new ModelAndView("student/course/form");
        if (id != null) {
            modelAndView.addObject("entity", service.findById(id));
        }
        return modelAndView;
    }

    @PostMapping("/all")
    public BaseResponse<?> all(SCourse lookup){
        List<SCourse> list = service.list(lookup);
        List<Select2VO> voList = list.stream().map(d -> {
            return new Select2VO(d.getId(), d.getName());
        }).collect(Collectors.toList());
        return BaseResponse.success(voList);
    }
}
